home *** CD-ROM | disk | FTP | other *** search
/ Chip: Chipnet / CHIPNET Aralık 1997.iso / linux / redhat / misc / src / install / smb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-11  |  4.5 KB  |  189 lines

  1. /*
  2.  *  smbmount.c
  3.  *
  4.  *  Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
  5.  *
  6.  *  Hacked by msf@redhat.com
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <pwd.h>
  14. #include <grp.h>
  15. #include <sys/socket.h>
  16. #include <sys/param.h>
  17. #include <netinet/in.h>
  18. #include <netdb.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. /* #include <sys/wait.h> */  /* generates a warning here */
  22. extern pid_t waitpid(pid_t, int *, int);
  23. #include <sys/errno.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <ctype.h>
  28. #include <stdlib.h>
  29. #include <sys/mount.h>
  30. #include <mntent.h>
  31.  
  32. #include <linux/fs.h>
  33. #include <linux/smb.h>
  34. #include <linux/smb_mount.h>
  35.  
  36. #include "log.h"
  37.  
  38.  
  39. /* probably have your own version of this? */
  40.  
  41. static void
  42. str_upper(char *name)
  43. {
  44.     while (*name)  {
  45.         *name = toupper(*name);
  46.         name = name + 1;
  47.     }
  48. }
  49.  
  50. /****************************************
  51.  Mount smb filesystem
  52.  
  53.  
  54.    server is name of SMB server - should NOT have domain name, etc 
  55.                                   as this is a NetBIOS name. For
  56.                                   machines running samba, is normally
  57.                                   the same as the hostname (sans domain)
  58.  
  59.    share is the name of the SMB share we are trying to mount
  60.  
  61.    user is the user to login as on SMB server.
  62.  
  63.    password is corresponding password.
  64.  
  65.    client is the NetBIOS name of machine attempting the mount. Just
  66.                  strip the hostname of domain to get this.
  67.  
  68.    mount_point - self explanatory
  69.  
  70. Way I'd do this - look at printtool (new one of course) and add a SMB printer.
  71.                   Ask for stuff just like I do.
  72.  
  73. End of docs 
  74. *****************************************/
  75.  
  76. int 
  77. smbmount(char *server, char *share, char *user, char *password, char *client, char *mount_point)
  78. {
  79.         struct smb_mount_data data;
  80.     char hostname[MAXHOSTNAMELEN + 1];
  81.     struct hostent * h;
  82.  
  83.         int um;
  84.     unsigned int flags;
  85.  
  86.     /*        
  87.     struct stat st;
  88.     unsigned int flags;
  89.     */
  90.  
  91.     memset(&data, 0, sizeof(struct smb_mount_data));
  92.  
  93.     memset(hostname, '\0', MAXHOSTNAMELEN+1);
  94.     gethostname(hostname, MAXHOSTNAMELEN);
  95.  
  96.     data.version = SMB_MOUNT_VERSION;
  97.  
  98.         /* getuid() gives us the real uid, who may umount the fs */
  99.         data.mounted_uid = getuid();
  100.  
  101.     sprintf(data.service, "\\\\%s\\%s", server, share);
  102.         str_upper(data.service);
  103.  
  104.         strcpy(data.root_path, "/");
  105.     strcpy(data.username, user);
  106.  
  107.     /* msf - this doesn't make sense during the install */
  108. #if 0
  109.         if (getenv("USER")) {
  110.                 strcpy(data.username, getenv("USER"));
  111.                 str_upper(data.username);
  112.         }
  113.  
  114.         if (data.username[0] == 0 && getenv("LOGNAME"))
  115.         {
  116.                 strcpy(data.username,getenv("LOGNAME"));
  117.                 str_upper(data.username);
  118.         }
  119. #endif
  120.  
  121.         data.max_xmit = 4070;   /* allocate at most one page in kernel */
  122.         data.uid = getuid();
  123.         data.gid = getgid();
  124.         um = umask(0);
  125.         umask(um);
  126.         data.file_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & ~um;
  127.         data.dir_mode  = 0;
  128.     data.addr.sin_family = AF_INET;
  129.     data.addr.sin_port = htons(SMB_PORT);
  130.  
  131.     /*    strcpy(data.domain, "?"); */
  132.  
  133.     if ((h = gethostbyname(server)) == NULL) {
  134.       logMessage("%s: unknown host", server);
  135.       return -1;
  136.     }
  137.     
  138.         data.addr.sin_addr.s_addr = ((struct in_addr *)(h->h_addr))->s_addr;
  139.     
  140.     data.fd = socket(AF_INET, SOCK_STREAM, 0);
  141.     if (data.fd == -1) {
  142.         logMessage("smb socket: %s", strerror(errno));
  143.                 return -1;
  144.     }
  145.     
  146.  
  147.         if (data.dir_mode == 0) {
  148.                 data.dir_mode = data.file_mode;
  149.                 if ((data.dir_mode & S_IRUSR) != 0)
  150.                         data.dir_mode |= S_IXUSR;
  151.                 if ((data.dir_mode & S_IRGRP) != 0)
  152.                         data.dir_mode |= S_IXGRP;
  153.                 if ((data.dir_mode & S_IROTH) != 0)
  154.                         data.dir_mode |= S_IXOTH;
  155.         }
  156.  
  157.     strcpy(data.password, password);
  158.  
  159.     str_upper(data.password);
  160.  
  161.         if (data.server_name[0] == '\0')
  162.     {
  163.                 if (strlen(server) > sizeof(data.server_name)-1)
  164.         {
  165.                         logMessage("server name too long as a netbios name");
  166.                         return -1;
  167.                 }
  168.                 strcpy(data.server_name, server);
  169.                 str_upper(data.server_name);
  170.         }
  171.  
  172.     
  173.     strcpy(data.client_name, client);
  174.   
  175.     flags = MS_MGC_VAL;
  176.  
  177.     if (mount(NULL, mount_point, "smbfs",
  178.                   flags, (char *)&data) < 0) {
  179.         logMessage("smb mount error: %s", strerror(errno));
  180.             close(data.fd);
  181.         return -1;
  182.     }
  183.  
  184.         close(data.fd);
  185.  
  186.  
  187.     return 0;
  188. }    
  189.